package com.mianshi;

import javax.sound.midi.Soundbank;
import java.util.Arrays;
import java.util.concurrent.locks.ReentrantLock;

public class guming extends ClassLoader{
    static StringBuffer sb = new StringBuffer();
    public static void main(String[] args) {
        String[] strArr = {"a","b","c"};
        char[] arr = new char[strArr.length];
        for(int i = 0; i < arr.length; i++){
            arr[i] = strArr[i].charAt(0);
        }
//        Arrays.sort(arr);
        cal(arr, 0, arr.length);
//        sb.deleteCharAt(sb.length()-1);
//        System.out.println();
        Solution solution = new Solution();
        String s = solution.strEnum(arr);
        System.out.println(s);
    }

    public static void cal(char[] strArr, int start, int end){
        if(end < 0){
            return;
        }
        if(start == end){
            sb.append(strArr);
            sb.append(",");
            System.out.println(strArr);
        }else{
            for(int i = start; i < end; i++){
                swap(strArr, i, start);
                cal(strArr, start+1,end);
                swap(strArr, start, i);
            }
        }
    }

    public static void swap(char[] arr, int i, int j){
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


    static class Solution {
        StringBuffer sb = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        boolean[] used;
        public String strEnum(char[] strArr) {
            used = new boolean[strArr.length];
            dfs(strArr, 0);
            return sb.toString();
        }

        public void dfs(char[] arr, int index){
            if(index == arr.length){
                sb.append(sb2.toString()+",");
                return;
            }
            for(int i = 0; i < arr.length; i++){
                if(used[i]) continue;
                if(i > 0 && arr[i] == arr[i-1] && !used[i-1]) continue;
                used[i] = true;
                sb2.append(arr[i]);
                dfs(arr, index+1); //这里是index加1
                used[i] = false;
                sb2.deleteCharAt(sb2.length()-1);
            }
        }
    }
}
